package nwpu.lizhiwen.system;


import nwpu.lizhiwen.houserent.domain.House;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ActivitySystem {
    private  Activity activities[] ;
    private int activityNumber = 3;
    Scanner scanner = new Scanner(System.in);

    //构造器，用于初始化
    public ActivitySystem(int size) {
        activities = new Activity[size];
        activities[0] = new ArtisticActivity("act_001", "合唱", "团体比赛", "翱翔学生中心", "8.7", "10.14", "歌唱");
        activities[1] = new LectureActivity("act_002", "翱翔讲座","人工智能主题专讲", "http://www.lecture.nwpu.edu.cn" ,"8.8", "8.15", "陈晶");
        activities[2] = new PartyActivity("act_003", "党日活动", "人人讲党课","http://www.dangri.nwpu.edu.cn","10.1", "12.2", "喜迎二十大", "党课");
    }

    //该方法用于找到某时间正在举办的活动
    public Activity[] getActivity( String findtime){
        int number = 0;
        Activity activity[] = new Activity[10];
        //将String类型转化为Date类型
        String str1 = findtime;
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM.dd.hh");
        Date time = null;
        try {
            time = sdf1.parse(str1);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //依次遍历比较活动是否符合要求
        //用before，after方法实现
        for (int i = 0; i < activityNumber; i++){
            if ((time.after(activities[i].getStartime())) && (time.before(activities[i].getEndtime()))) {
               activity[number++] = activities[i];
            }
        }
        return activity;
    }

    //该方法用于找到activities数组中的对应activities并传出
    public Activity find(String findId){
        for (int i = 0; i < activityNumber; i++){
            if (findId.equals(activities[i].getId()) ){
                return activities[i];
            }
        }
        return null;
    }

    //此方法用于删除活动
    public boolean del(String delId){
        int index = -1;
        //先用for循环找到要删除的活动
        for (int i = 0;i <activityNumber; i++){
            if (delId .equals(activities[i].getId()) ){
                index = i;
            }
        }
        //没找到就传回false
        if (index == -1){
            return false;
        }
        //若找到该活动则将该元素之后的元素整体前移一位，并将移动后的最后一位元素，也就是要删除的元素赋为空
        for (int i = index; i < activityNumber-1; i++){
            activities[i] = activities[i+1];
        }
        activities[activityNumber-1] = null;
        //数组长度减一
        activityNumber--;
        return true;
    }

    //该方法用于添加活动
    public boolean add(Activity activity){
        if (activityNumber == activities.length){
            System.out.println("数组已满，不可再添加");
            return false;
        }
        //将传进来的activity添加到定义的activities数组中
        activities[activityNumber++] = activity;
        return true;
    }

    //传回所有的Activity信息
    public Activity[] show(){
        return activities;
    }
}
